package bTree.LeetCode;

import java.util.Stack;

public class 最长有效括号 {
    public int longestValidParentheses(String s) {
        int length = s.length();
        Stack<Integer>stack=new Stack<>();
        boolean []path=new boolean[length];
        for (int i = 0; i < length; i++) {
            char c = s.charAt(i);
            if (c=='('){
                stack.add(i);
            }else {
                if (!stack.isEmpty()){
                    Integer pop = stack.pop();
                    path[pop]=true;
                    path[i]=true;
                }
            }
        }
        int res=0;
        int cnt=0;
        for (boolean b : path) {
           if (b){
               cnt++;
           }
            if (!b){
                cnt=0;
            }
            res=Math.max(res,cnt);
        }
        return res;
    }
}